// utils/index.js

/**
 * 页面底部间距工具
 */
const PageBottomPadding = {
  /**
   * 获取底部导航栏高度
   */
  getHeight() {
    try {
      // 优先使用新的API
      const deviceInfo = wx.getDeviceInfo();
      const windowInfo = wx.getWindowInfo();
      const safeAreaBottom = (windowInfo.safeAreaInsets?.bottom || 0) * 2;
      const baseHeight = 100; // 100rpx
      return baseHeight + safeAreaBottom;
    } catch (error) {
      // 降级到旧API
      const systemInfo = wx.getSystemInfoSync();
      const baseHeight = 100; // 100rpx
      const safeAreaBottom = (systemInfo.safeAreaInsets?.bottom || 0) * 2;
      return baseHeight + safeAreaBottom;
    }
  },

  /**
   * 获取CSS样式字符串
   */
  getStyle() {
    const height = this.getHeight();
    return `padding-bottom: ${height}rpx;`;
  },

  /**
   * 设置页面底部间距
   * @param {Object} pageInstance 页面实例
   */
  setPagePadding(pageInstance) {
    const style = this.getStyle();
    pageInstance.setData({
      pageBottomStyle: style
    });
  }
};

/**
 * 页面混入 - 自动处理底部间距
 */
const PageBottomMixin = {
  data: {
    pageBottomStyle: ''
  },

  onLoad() {
    this.setPageBottomPadding();
  },

  onShow() {
    this.setPageBottomPadding();
  },

  methods: {
    setPageBottomPadding() {
      PageBottomPadding.setPagePadding(this);
    }
  }
};

// 导出
module.exports = {
  PageBottomPadding,
  PageBottomMixin
};
